﻿
//父控制器
app.controller("pageCtrl", function($scope) {
	//安全apply
	//	$scope.safeApply = function(fn) {
	//	    var phase = this.$root.$$phase;
	//	    if (phase == '$apply' || phase == '$digest') {
	//	        if (fn && (typeof(fn) === 'function')) {
	//	            fn();
	//	        }
	//	    } else {
	//	        this.$apply(fn);
	//	    }
	//	};
	
	
	
	$scope.callSuccessHttp = function() {
//		if ($scope.uiObj.pageloadingCtrl) {
//			$scope.uiObj.loading = false;
//			$scope.uiObj.isactive = 'active';
//		}
//		$scope.uiObj.pageloadingCtrl = true;

		$scope.uiObj.loading = false;

		
		$scope.uiObj.isactive = 'active';
		$scope.uiObj.layerloadingCtrl=false;
		layer.closeAll();
		
	}
	
	$scope.showOrhide = function() {
			var height;
			if ($scope.sqMore.isshow == 'sq-hide') {
				$scope.sqMore.isshow = 'sq-active';

			} else {
				$scope.sqMore.isshow = 'sq-hide';

			}
			$scope.$apply();
		}
	$scope.sqChat={
		isshow:''
	}
		//右上角更多菜单参数
	$scope.sqMore = {
			isshow: 'sq-hide'
		}
		//侧边栏参数
	$scope.sqSide = {
			isshow: 'sq-hide'
		}
		//遮罩参数
	$scope.sqMask = {
			isshow: 'sq-hide'
		}
		//发帖窗口
	$scope.sqCom={
		isshow:''
	}
		//各个控件参数初始化
	$scope.uiInit = function() {
		$scope.sqMore = {
			isshow: 'sq-hide'
		}
		$scope.sqSide = {
			isshow: 'sq-hide'
		}
		$scope.sqMask = {
			isshow: 'sq-hide'
		}
	}
	//每个页面的顶部栏参数
	$scope.page={
		topOpen:true,
		title:'',
		leftNav:'',
		rightNav:''
	}
	
	
});

//首页--控制器
app.controller("IndexCtrl", function($scope, $http) {
	$scope.uiObj.pageAnimationIs=false;
	$scope.uiInit();
	$scope.page.topOpen=true;
	$scope.page.title='密友圈';
	$scope.page.leftNav='sqside';
	$scope.page.rightNav='sqmore';
	$scope.showlist=false;
	
	var _url = buildUrl('json', 'community', 'index', {
		qid: document.getElementById('qid').value
	})
	var facePage=function(response){
		//定义一个存放每页表情的数组
		$scope.uiObj.facebox=new Array();
		//获取表情数据,数据长度,页数
		var facebox=response.facebox,
			len=facebox.length,
			page=parseInt(len / 24);
		//循环数据放置表情
		for(var i=0;i<=page;i++)
		{
			$scope.uiObj.facebox[i]=new Array();
			
			for(var y=(i*24);y<(i+1)*24;y++)
			{
				response.facebox[y].url=res_Url+response.facebox[y].url;
				$scope.uiObj.facebox[i].push(response.facebox[y]);
				if(y==(len-1))
				{
					break;
				}
			}
		}
		
		
		
	}
	$http.get(_url).success(function(response) {



		facePage(response);

		$scope.data = response;


		$scope.callSuccessHttp();
		$scope.showlist=true;
	});
	
});


//文章和帖子--控制器
app.controller('sqContentlist', function($scope, $http) {
	if($scope.data.selfInfo!=null)
	{
		var uid=$scope.data.selfInfo.uid
	}
	else
	{
		var uid='';
	}
	
	$scope.pageUi = {
				uid:uid,
				isTopCount:0,
				isFirst:true,
				text:'下拉加载',
				start: 0,
				limit: 20,
				isWork:true,
				myIdentity:document.getElementById('myIdentity').value,
				//局部变更某一条目的数据 根据点赞
				updateListByPraise:function(index,self){
					
					$scope.data.content[index].praise=self;
					if($scope.data.content[index].selfPraise==0)
					{
						$scope.data.content[index].selfPraise=1;
					}
					else
					{
						$scope.data.content[index].selfPraise=0;
					}
				},
				//局部变更某一条目的数据 根据置顶
				updateListByTop:function(index){
					if($scope.data.content[index].isTop!=null)
					{
						$scope.data.content[index].isTop=null;
					}
					else
					{
						$scope.data.content[index].isTop=1;
					}
				},
				//去读取某一条评论记录并且刷新
				updateList:function(index){
					var _link=buildUrl('json','community','contentList',{
						qid:document.getElementById('qid').value,
						start:index,
						limit:1
					})
					$http.get(_link).success(function(response){
						console.log(response);
						console.log(index);
						$scope.data.content[index].comment=response.content[0].comment;
						$scope.callSuccessHttp();
						//清除遮罩里的全部内容
						var ele=document.getElementsByClassName('sq-mask');
						ele=angular.element(ele);

						ele.html('');
						$scope.uiInit();
						
					});
				}
				
	}		
//	var _link = buildUrl('json', 'community', 'contentList', {
//		qid: document.getElementById('qid').value,
//		start: $scope.pageUi.start,
//		limit: $scope.pageUi.limit
//	})
//
//	console.log(_link);
//	$scope.uiObj.pageloadingCtrl=false;
//	$scope.pageUi.text='正在加载...';
//	$http.get(_link).success(function(response) {
//		if (response.content!=null) {
//			console.log(response);
//			$scope.data = response;
//			$scope.pageUi.start = $scope.pageUi.start + $scope.pageUi.limit;
//		}
//		$scope.callSuccessHttp();
//		$scope.pageUi.text='下拉加载';
//	});


})

//发帖上传照片控制器
app.controller('sqUploadimg',function($scope,$http){
	$scope.wxImgs={
		upload:true,
		paths:'',
		imgs:[]
	}
	$scope.uploadimgWx=function(){
					console.log($scope);
					wx_share.uploadMultImage(function(data){
//						alert(data.path);
						if($scope.wxImgs.imgs.length<9)
						{
							$scope.wxImgs.imgs.push({
								url:data.path,
								isshow:true
							})
							if($scope.imgs.paths=='')
							{
									$scope.imgs.paths=data.path;
							}
							else
							{
									$scope.imgs.paths=$scope.imgs.paths+','+data.path;
							}
							if($scope.wxImgs.imgs.length==9)
							{
								$scope.wxImgs.upload=false;
							}
							
						}
						$scope.$apply();
					});
				
				
	}
})

//首页菜单
app.controller('sqIndexmenu',function($scope,$http){
	$scope.goiframe=function(_link){
		console.log(_link)
	}
})

//文章
app.controller('ArticleCtrl',function($scope,$rootScope,$routeParams) {
	$scope.page.topOpen=false;
	$scope.page.title='文章';
	$scope.page.leftNav='sqbackcom';
	$scope.page.rightNav='';
	$scope.id = $routeParams.id;
	$scope.qId = $routeParams.qId;
	$scope.miUId = $routeParams.miUId;
//	alert($scope.id+','+$scope.qId+','+$scope.miUId);
	var _link=buildUrl('html','micontent','getMiContentDetail',{
		qId:document.getElementById('qid').value,
		id:$scope.id,
		miUId:$scope.miUId
	})
	$scope.url=serhost+_link;
	console.log($scope)
	$scope.callSuccessHttp();
//	$rootScope.loadLoyer();
});

//用户推广
app.controller('ExtensionCtrl',function($scope,$rootScope,$routeParams,$http) {
	$scope.uiInit();
	$scope.page.topOpen=true;
	$scope.page.title='锁定用户';
	$scope.page.leftNav='sqbackcom';
	$scope.page.rightNav='';
	$scope.qId = $routeParams.qId;
	
	var _default={
		start:0,
		limit:20
	}
	
	$scope._params={
		cid:null,
		type:null,
		vid:null,
		tagId:null,
		sort:null
	}
	
	$scope.pageUi={
		content:[{
			name:'供应商',
			selectNav:"sq-hide",
			list:new Array()
		},
		{
			name:'通过链接锁定的用户',
			selectNav:"sq-hide",
			list:[{name:"通过链接锁定的用户",type:0},{name:"通过二维码锁定的用户",type:1}]
		}],
		data:"",
		cid:null,
		type:null,
		mask:'sq-hide',
		mallload:false,
		activeselect:0,
		dataType:0,
		isFirst:true,
		isWork:true,
		start:_default.start,
		limit:_default.limit,
		init:function(){
			$scope.pageUi.start=_default.start;
			$scope.pageUi.limit=_default.limit;
			$scope.pageUi.isFirst=true;
			$scope.pageUi.isWork=true;
		}
		
	}
//	alert($scope.id+','+$scope.qId+','+$scope.miUId);
	var _link=buildUrl('json','community','userPromotion',{
		qid:document.getElementById('qid').value
	})
	
	$http.get(_link).success(function(response){
		$scope.pageUi.content[0].list=response.vendorList;
		$scope.pageUi.content[0].name=response.vendorList[0].name;
		$scope.pageUi.cid=response.vendorList[0].customerId;
		$scope.pageUi.vid=response.vendorList[0].id;
		console.log(response)
		
		$scope.callSuccessHttp();
		
	})
	
	
	
	$scope.showMask=function(){
		$scope.pageUi.mask="sq-active";
		
	}
	$scope.hideMask=function(){
		$scope.pageUi.mask="sq-hide";
		
	}
//	$rootScope.loadLoyer();
});
app.controller('qcodeCtrl',function($scope,$rootScope,$routeParams,$http) {
	$scope.uiInit();
	$scope.page.topOpen=true;
	$scope.page.title='我的海报';
	$scope.page.leftNav='sqbackcom';
	$scope.page.rightNav='';

	
	$scope.id = $routeParams.id;
	var _link=buildUrl('json','community','getPromotionCode',{
		mallId:$scope.id,
		qid:document.getElementById('qid').value
	})
	
	$http.get(_link).success(function(response){
		$scope.data=response;
		console.log(response)
		$scope.callSuccessHttp();
		
	})
});
app.controller('proqcodeCtrl',function($scope,$rootScope,$routeParams,$http){
	$scope.uiInit();
	$scope.page.topOpen=false;
	$scope.pid = $routeParams.pid;
	$scope.ishide=true;
	var _link=buildUrl('json','community','getProductCode',{
		qid:document.getElementById('qid').value,
		pid:$scope.pid
	})
	
	$http.get(_link).success(function(response){
		$scope.data=response;
		console.log(response)
		$scope.callSuccessHttp();
		
	})
	
	$scope.hidemask=function(){
		$scope.ishide=false;
	}
})
app.controller('bulproductCtrl',function($scope,$rootScope,$routeParams,$http) {
	$scope.uiInit();
	$scope.page.topOpen=true;
	$scope.page.title='产品分销列表';
	$scope.page.leftNav='sqbackcom';
	$scope.page.rightNav='';
	
	$scope._params={
		cid:'',
		type:'',
		vid:'',
		tagId:'',
		sort:''
	}
	var _default={
		start:0,
		limit:20
	}
	
	$scope.pageUi={
		content:[{
			name:'商城',
			selectNav:"sq-hide",
			list:new Array()
		},
		{
			name:'商品分类',
			selectNav:'sq-hide',
			list:new Array()
		},
		{
			name:'排序',
			selectNav:"sq-hide",
			list:[
				{name:"缺省排序",type:0},
				{name:"最新产品",type:1},
				{name:"返利最高",type:2},
				{name:"销量最高",type:3},
				{name:"价格最高",type:4}
			]
		}],
		isFirst:true,
		dataType:1,
		vid:null,
		tagId:null,
		sort:null,
		mask:'sq-hide',
		mallload:false,
		activeselect:0,
		isWork:true,
		isupdate:false,
		selectinit:false,
		start:_default.start,
		limit:_default.limit,
		keyName:'',
		init:function(){
			$scope.pageUi.start=_default.start;
			$scope.pageUi.limit=_default.limit;
			$scope.pageUi.isFirst=true;
			$scope.pageUi.isWork=true;
		}
	}
	
	var _link=buildUrl('json','community','distributionProduct',{
		qid:document.getElementById('qid').value
	})
	
	$http.get(_link).success(function(response){
		$scope.pageUi.content[0].list=response.vendorList;
		$scope.pageUi.content[1].list=response.distributionList;
		if($scope.pageUi.content[0].list)
		{
			$scope.pageUi.content[0].list.unshift({
				id:'',
				name:'全部',
				logo:1
			})
		}
		
		if($scope.pageUi.content[1].list)
		{
			$scope.pageUi.content[1].list.unshift({
				id:'',
				name:'全部',
				logo:1
			})
		}
		
		console.log(response)
		$scope.callSuccessHttp();
		
	})
	//产品搜索
	$scope.prosearch=function(ev){
		if (ev.keyCode !== 13) return; 
		var name=$scope.pageUi.keyName;
		
		
		$scope.pageUi.data.productList='';
		$scope.pageUi.init();
		$scope.pageUi.text='正在加载...';
		$scope.pageUi.mallload=true;
		
		
		var _link=buildUrl('json','community','getProductByCondition',{
			name:name,
			qid:document.getElementById('qid').value,
			vid:$scope._params.vid!=null?$scope._params.vid:'',
			tagId:$scope._params.tagId!=null?$scope._params.tagId:'',
			sort:$scope._params.sort!=null?$scope._params.sort:0,
			start:$scope.pageUi.start,
			limit:$scope.pageUi.limit
		})
		
		$http.get(_link).success(function(response){
			if(response.productList)
			{
				if($scope.pageUi.isFirst)
				{
					$scope.pageUi.data.productList=response.productList;
					$scope.pageUi.isFirst=false;
					if(response.productList.length<$scope.pageUi.limit)
					{
						$scope.pageUi.isWork=false;
						$scope.pageUi.text='没有更多数据';
					}
				}
				else
				{
					for(var i=0;i<response[KEY].length;i++)
						{
							$scope.pageUi.data.productList.push(response.productList[i]);
						}
				}
				
			}
			else
			{
				$scope.pageUi.text='没有更多数据';
			}
			$scope.pageUi.mallload=false;
			$scope.callSuccessHttp();
		})
		
	}
});
//我的收入
app.controller('balancesumCtrl',function($scope,$rootScope,$routeParams,$http,$cacheFactory) {
	$scope.uiInit();
	$scope.page.topOpen=true;
	$scope.page.title='我的收入';
	$scope.page.leftNav='sqbackcom';
	$scope.page.rightNav='';
	$scope.btype = $routeParams.type;
	var defaultstart=0,
		defaultlimit=10;
	$scope.pageUi={
		balancelist:[
			{
				start:defaultstart,
				limit:defaultlimit,
				iswork:true,
				tType:1,
				tabName:"收入",
				tabId:"tType1",
				menu:["时间","事由","金额"]
			},
			{
				start:defaultstart,
				limit:defaultlimit,
				iswork:true,
				tType:2,
				tabName:"支出",
				tabId:"tType2",
				menu:["时间","事由","金额"]
			},
			{
				start:defaultstart,
				limit:defaultlimit,
				iswork:true,
				tType:3,
				tabName:"提现",
				tabId:"tType3",
				menu:["时间","状态","金额"]
			}
		]
	}
	var myIdentity=document.getElementById('myIdentity').value;
	if(myIdentity==0)
	{
		var _uType=2;
	}
	else if(myIdentity==1)
	{
		var _uType=1;
	}
	var _link=buildUrl('json','quan','getBalanceSum',{
		qId:document.getElementById('qid').value,
		uType:1
	})
	$http.get(_link).success(function(response){
		$scope.data=response;
		
		$scope.callSuccessHttp();
	});
});
//我要提现
app.controller('saveCashCtrl',function($scope,$rootScope,$routeParams,$http,$cacheFactory) {
	$scope.page.topOpen=false;
	$scope.cashsuccess=false;
	$scope.cashNum=null;
	
	var myIdentity=document.getElementById('myIdentity').value;
	if(myIdentity==0)
		{
			var _uType=2;
		}
		else if(myIdentity==1)
		{
			var _uType=1;
		}
	var _link=buildUrl('json','quan','getBalanceSum',{
		qId:document.getElementById('qid').value,
		uType:1
	})
	$http.get(_link).success(function(response){
		$scope.data=response;
		$scope.callSuccessHttp();
	});
	
	$scope.saveCash=function(){
		var _link=buildUrl('json','quan','saveCash',{
			qId:document.getElementById('qid').value,
			uType:1,
			cashNum:$scope.cashNum
		})
		$scope.uiObj.layerloadingCtrl=true;
		if($scope.cashNum==null)
			{
				layer.open({
					content:'请输入提现金额'
				})
			}
		else
		{
			$http.get(_link).success(function(response){
				console.log(response);
				$scope.callSuccessHttp();
				if(response.code==0)
				{
					$scope.cashsuccess=true;
				}
				else
				{
					layer.open({
						content:response.msg
					})
				}
				
			});
		}
	}
});
//邀请
app.controller('refereeInfoCtrl',function($scope,$rootScope,$routeParams,$http) {
	$scope.uiInit();
	$scope.page.topOpen=true;
	$scope.page.title='邀请有奖';
	$scope.page.leftNav='sqbackcom';
	$scope.page.rightNav='';
	
	var _default={
		start:0,
		limit:20
	}
	
	$scope.pageUi={
		text:"正在加载..",
		start:_default.start,
		limit:_default.limit,
		iswork:true,
		data:null,
		isShare:false,
		mask:false,
		maskUrl:document.getElementById('resUrl').value+'wx_assets/community/images/share.png'
	}
	$scope.refeshowMask=function(){
		$scope.pageUi.mask=true;
	}
	$scope.refehideMask=function(){
		$scope.pageUi.mask=false;
	}
});

app.controller("teamCtrl",function($scope,$rootScope,$routeParams,$http){
	$scope.teamId = $routeParams.teamId;
	$scope.uiInit();
	$scope.page.topOpen=true;
	$scope.page.title='我的团队';
	$scope.page.leftNav='sqbackcom';
	$scope.page.rightNav='';
	var time = new Date().getTime();
	if($rootScope.uiObj.pageAnimationIs)
	{
		$rootScope.uiObj.pageAnimationClass='slideRight';
		setTimeout(function(){
			$rootScope.uiObj.pageAnimationClass='';
			$scope.$apply();
		},300)
	}
	var _default={
		start:0,
		limit:20
	}
	
	$scope.pageUi={
		clickactive:true,
		text:"正在加载..",
		start:_default.start,
		limit:_default.limit,
		iswork:true,
		data:null,
		pageType:'team'
	}
	
	var _link=buildUrl('json','community','quanTeam',{
		teamId:document.getElementById('teamId').value,
	})
	$http.get(_link).success(function(response){
		$scope.data=response;
		shareData.link = document.getElementById('serhost').value+'call.html?customerId='+document.getElementById('customerId').value+
		"&module=community&method=inviteJoinTeam&params=%7B%22id%22:"+document.getElementById('teamId').value+",%22share%22:"+time+"%7D";
		shareData.title=response.nickName+"邀请您加入团队";
		shareData.desc="加入团队，我们一起变得更强大！";
		shareData.imgUrl = response.headImgUrl;
		wx_share.init(shareData);
		$scope.callSuccessHttp();
	});

})

app.controller("myfansCtrl",function($scope,$rootScope,$routeParams,$http){
	
	$scope.uiInit();
	$scope.page.topOpen=true;
	$scope.page.title='我的好友';
	$scope.page.leftNav='sqbackcom';
	$scope.page.rightNav='';
	$scope.hhrtag=document.getElementById('hhrtag').value;
	$scope.mytag=document.getElementById('mytag').value;
	if($rootScope.uiObj.pageAnimationIs)
	{
		$rootScope.uiObj.pageAnimationClass='slideRight';
	}
	var _default={
		start:0,
		limit:20
	}
	
	$scope.pageUi={
		clickactive:true,
		text:"正在加载..",
		start:_default.start,
		limit:_default.limit,
		iswork:true,
		data:null,
		pageType:'myfans',
		refereeAward:document.getElementById('refereeAward').value,
		parentRefereeAward:document.getElementById('parentRefereeAward').value,
		fansCount:$rootScope.uiObj.fansCount
	}
	$scope.callSuccessHttp();
});
app.controller("treepageCtrl",function($scope,$rootScope,$routeParams,$http,$cacheFactory){
	
	
	$scope.type=$routeParams.type;
	$scope.uiInit();
	$rootScope.uiObj.pageAnimationIs=true;
	if($rootScope.uiObj.pageAnimationForce)
	{
		$rootScope.uiObj.pageAnimationClass='slideLeft';
	}
	else 
	{
		$rootScope.uiObj.pageAnimationClass='slideRight';
		setTimeout(function(){
			$rootScope.uiObj.pageAnimationClass='slideLeft';
			$scope.$apply();
		},300)
	}
	$rootScope.uiObj.pageAnimationForce=false;
	$scope.page.topOpen=true;
	if($scope.type=="team")
	{
		$scope.page.title='我的团队';
	}
	else if($scope.type=="myfans")
	{
		if($rootScope.uiObj.fansTitleCache.get('uid:'+$routeParams.uid))
		{
			$scope.page.title=$rootScope.uiObj.fansTitleCache.get('uid:'+$routeParams.uid)
		}
		else if(!$scope.page.title)
		{
			$scope.page.title='我的好友';
		}
	}
	
	$scope.page.leftNav='sqbackcom';
	$scope.page.rightNav='';
	$rootScope.animateActive=true;
	$scope.callSuccessHttp();
	
	var _default={
		start:0,
		limit:20
	}
	
	$scope.pageUi={
		clickactive:true,
		text:"正在加载..",
		start:_default.start,
		limit:_default.limit,
		iswork:true,
		data:null
	}
});